/**
 * Created by IntelliJ IDEA.
 * User: Alex
 * Date: 4/27/11
 * Time: 5:11 PM
 * To change this template use File | Settings | File Templates.
 */
var stars;
var images;
/*
a function to preload images to the web page to reduces lag times
and future network traffic
 */
function preloadimages(){
	var i=0;
	imageObj = new Image();
    images = new Array();
	images[0]="/FilmRing/images/0stars.jpg";
	images[1]="/FilmRing/images/1stars.jpg";
	images[2]="/FilmRing/images/2stars.jpg";
	images[3]="/FilmRing/images/3stars.jpg";
	images[4]="/FilmRing/images/4stars.jpg";
	images[5]="/FilmRing/images/5stars.jpg";
    imageObjs = new Array()
	for(i=0; i<=5; i++){
		$('<img />')[0].src =images[i];

	}
}
/*
registers items with the rating class and the index as listeners for mouse
events. On mouse movement when over the element it examines the mouse position
relative to the image and changes the image to match the current position fo the mouse.
On mouse leave it resets to the image to that user's current rating of the movie or
the default no rating image. On click it changes the user's current rating for that
specific element and performs an ajax call to alert the server that the user has
rated the movie with the value determined by mouse position at the time of click.
 */
function registerRatingsListeners2(index, rating, userid, selector){
    $($(".rating")[index]).data("rating", rating);
    $(".rating img")[index].onmousemove=function(event){
        var width = $($(".rating img")[index]).width();
        $($(".rating img")[index]).attr("src",(images[(Math.floor((5*((event.pageX-$($(".rating img")[index]).position().left)/width)))+1)]));
    };
    $(".rating img")[index].onmouseout=function(event){
        var defaultRating=$($(".rating")[index]).data("rating");
        $($(".rating img")[index]).attr("src", images[defaultRating]);
    };
    $(".rating img")[index].onclick=function(event){
        var width = $($(".rating img")[index]).width();
		$($(".rating")[index]).data("rating",(Math.floor((5*((event.pageX-$($(".rating img")[index]).position().left)/width)))+1));
        $.ajax({
            url:"/FilmRing/rating/rate",
            type:"POST",
            dataType:"html",
            data:{
                userId:userid,
                rating:$($(".rating")[index]).data("rating"),
                movieId:$(".rating")[index].id.split('/').pop()
            }
        });
    };
}

/*
possibly unused doesn't handle multiple rating widgets
 */

function registerRatingsListeners(selector, rating, userid){
    alert(selector+" "+rating+" "+userid);
    alert(""+selector+" "+$(selector).toSource()+" "+$(selector+" img").toSource());
    $(selector).data("rating",rating);
    $(selector+" img").mousemove(function(event) {
        //alert("mouse move event");
		var width = $(selector+" img").width();
        $(selector+" img").attr("src",(images[(Math.floor((5*((event.pageX-$(selector+" img").position().left)/width)))+1)]));
	});
	$(selector+" img").mouseleave(function(event) {
		//alert("mouse leave event");
        var defaultRating=$(selector).data("rating");
		$(selector+" img").attr("src",images[defaultRating]);
	});
	$(selector+" img").click(function(event) {
        //alert("mouse click event");
		var width = $(selector+" img").width();
		$(selector).data("rating",(Math.floor((5*((event.pageX-$(selector+" img").position().left)/width)))+1));
        $.ajax({
            url:"/FilmRing/rating/rate",
            type:"POST",
            dataType:"html",
            data:{
                userId:userid,
                rating:$(selector).data("rating"),
                movieId:$(selector).attr("id").split('/').pop()
            }
        });
	});
}
